package com.zh.modules.app.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.zh.common.utils.Constants;
import com.zh.common.utils.RedisUtils;
import com.zh.modules.app.param.RegisterParam;
import com.zh.modules.app.param.SendCodeForm;
import com.zh.modules.app.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public String sendEmailCode(SendCodeForm request) {
        String code = RandomUtil.randomNumbers(Constants.SMS_SIZE);
        String codeKey = "code_" + request.getEmail();
        log.info("sendEmailCode:{},key={}", code, codeKey);
        redisUtils.set(codeKey, code, 300);
        return code;
    }

    @Override
    public Boolean checkEmailCode(RegisterParam registerParam) {
        String codeKey = "code_" + registerParam.getUsername();
        String code = redisUtils.get(codeKey);
        log.info("checkEmailCode:{},key={}", code, codeKey);
        if (registerParam.getCode().equals(code)) {
            return true;
        }
        return false;
    }
}
